package com.wangku.springmvc.common;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wangku.springmvc.service.WxTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.wangku.springmvc.entity.PageData;

import java.util.Map;

public class BaseController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private WxTokenService wxTokenService;

    /**
	 * 得到ModelAndView
	 */
	public ModelAndView getModelAndView() {
		return new ModelAndView();
	}

	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();

		return request;
	}

	/**
	 * 得到request对象
	 */
	public HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}


	/**
	 * 得到PageData
	 */
	public PageData getPageData() {
		PageData pData = new PageData(this.getRequest());
		return pData;
	}

	/**
	 * 获取用户从session
	 * 
	 * @param
	 * @throws Exception
	 */

	public PageData findUser() throws Exception {
		HttpServletRequest request = this.getRequest();
		HttpSession session = request.getSession();
		PageData data = (PageData) session.getAttribute("SHOPUSERSESSION");
		return data;
	}

	/**
	 * 保存用户到session
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void saveUser(PageData pd) throws Exception {
		HttpServletRequest request = this.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute("SHOPUSERSESSION", pd);
	}

	/**
	 * 清除用户到session
	 * 
	 * @throws Exception
	 */
	public void clear() throws Exception {
		HttpServletRequest request = this.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute("SHOPUSERSESSION");
	}

	/**
	 * 获取连接全路径
	 * 
	 * @return
	 */
	public String getURL() {
		HttpServletRequest request = this.getRequest();
		StringBuffer url = request.getRequestURL();
		if (request.getQueryString() != null) {
			url.append("?");
			url.append(request.getQueryString());
		}
		return url.toString();
	}

	/**
	 * 获取小程序用户信息
	 * @return pageData
	 */
	 public PageData getWxUserInfo(){
         HttpServletRequest request = this.getRequest();
         String token = request.getParameter("deviceToken");
         Map<String, String> map = wxTokenService.getToken(token);
         PageData pd = new PageData();
         if (!map.isEmpty()) {
             pd.put("DeviceToken",map.get("deviceToken"));
             pd.put("OpenId",map.get("OpenId"));
             pd.put("UserName",map.get("UserName") );
             pd.put("UnionId",map.get("UnionId"));
         }
         return pd;
	 }
	
}
